package Connector;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import Components.RoomsPanel;
import server.*;
import smash_tris.*;

/**
 * Klasa reprezentuje połączenie pomiędzy klientem a serwerem.
 * 
 * @author Maciej Graszek
 * @author Pawel Rychly
 * 
 */
public class Connector /* implements Runnable */{

	private Socket clientSocket = null;
	private Socket broadcastMessagesSocket = null;

	private ObjectInputStream in = null;
	private ObjectOutputStream out = null;
	private ObjectInputStream broadCastIn = null;
	//private ObjectOutputStream broadCastOut = null;

	
	private String adres = "localhost";
	private boolean isRunning = false;

	private int portNumber = 3001;
	private int broadCastPortNumber = portNumber + 1;
	private InetAddress adr;
	private Player player = null;
	
	private RoomsPanel roomsPanel = null;

	public Connector() throws IOException {

		try {
			clientSocket = new Socket(adr.getLocalHost(), portNumber);
			System.out.println("połączono");
			this.broadcastMessagesSocket = new Socket(adr.getLocalHost(),
					broadCastPortNumber);
			System.out.println("połączono");
			out = new ObjectOutputStream(new BufferedOutputStream(
					clientSocket.getOutputStream()));
			out.flush();

			in = new ObjectInputStream(new BufferedInputStream(
					clientSocket.getInputStream()));

			System.out.println("socket ok");

			
			broadCastIn = new ObjectInputStream(new BufferedInputStream(
					broadcastMessagesSocket.getInputStream()));
			System.out.println("broad cast socket ok");
			

		} catch (UnknownHostException e) {
			System.err.println("Don't know about host: localhost.");
			System.exit(1);
		} catch (IOException e) {
			System.err.println("Couldn't get I/O for "
					+ "the connection to: taranis.");
			System.exit(1);
		}

		/*
		 * Thread t = new Thread(this); this.isRunning = true; t.start();
		 */
		/*
		 * BufferedReader stdIn = new BufferedReader( new
		 * InputStreamReader(System.in));
		 */
	}

	/*
	 * @Override public void run() { DataPacket userInput; while (isRunning) {
	 * try { while ((userInput = (DataPacket) in.readObject()) != null) {
	 * System.out.println(userInput.getTypeOfInformation());
	 * System.out.println("wyświetlono"); } } catch (ClassNotFoundException e) {
	 * // TODO Auto-generated catch block e.printStackTrace(); } catch
	 * (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
	 * } }
	 * 
	 * }
	 */

	public ArrayList<Room> getAllRooms() {
		ArrayList<Room> rooms = null;
		try {
			this.out.writeObject(new DataPacket(
					TypeOfDataPacket.GET_LIST_OF_ROOMS, null));
			this.out.flush();
			DataPacket userInput = null;
			userInput = (DataPacket) in.readObject();
			rooms = (ArrayList<Room>) userInput.getData();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return rooms;
	}

	public Player createPlayer(String nick) {
		Player player = null;

		try {
			this.out.writeObject(new DataPacket(TypeOfDataPacket.ADD_ROOM, nick));
			this.out.flush();
			DataPacket userInput = null;
			userInput = (DataPacket) in.readObject();
			player = (Player) userInput.getData();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.player = player;
		return player;
	}

	/**
	 * Wysyła do serwera żądanie utworzenia nowego pokoju.
	 * 
	 * @return Aktualna lista pokoi.
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Room> createRoom() {
		if (this.player != null) {
			ArrayList<Room> rooms = null;
			try {

				this.out.writeObject(new DataPacket(TypeOfDataPacket.ADD_ROOM,
						new Room(this.player)));
				this.out.flush();
				DataPacket userInput = null;
				userInput = (DataPacket) in.readObject();
				rooms = (ArrayList<Room>) userInput.getData();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return rooms;
		} else {
			return null;
		}

	}

	public void close() {
		this.isRunning = false;
		System.out.println("koniec");
		try {
			out.close();
			in.close();
			clientSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * Metoda przypisuje referencje do obiektu RoomsPanel.
	 * @param roomsPanel
	 */
	public void setRoomsPanel(RoomsPanel roomsPanel) {
		this.roomsPanel = roomsPanel;
	}
	
}
